package arrays;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description: 209.长度最小的子数组
 * User: Li_yizYa
 * Date: 2025—03—20
 * Time: 20:37
 */
public class Solution4 {
    private static int minSubArray(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int ret = Integer.MAX_VALUE;
        int left = 0;
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            while (sum >= target) {
                ret = Math.min(ret, i - left + 1);
                sum -= nums[left++];
            }
        }
        return ret == Integer.MAX_VALUE ? 0 : ret;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入数组的大小: ");
        int n = scan.nextInt();
        System.out.print("请输入数组元素: ");
        int[] nums = new int[n];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = scan.nextInt();
        }
        System.out.print("请输入目标值: ");
        int target = scan.nextInt();
        System.out.println("大于等于target的最小子数组大小为: " + minSubArray(nums, target));
    }
}
